EEL_MODS	+= rootfs

# update RFS_IMAGE
$(RFS_IMAGE): $(RFS_DEP_$(RFS_MODE))
	$(EELQ5)$(MAKE_THIS) rootfs

PHONY-rootfs += rootfs rootfs-info
rootfs: rootfs_$(RFS_MODE)
	$(EELQ5)$(MAKE_THIS) rootfs-info

rootfs-info:
	@echo "RFS_MODE: $(RFS_MODE)"
	@echo "RFS_IMAGE: $(RFS_IMAGE)"

PHONY-rootfs += rootfs_bb rootfs_br rootfs_bs
rootfs_bb: rootfs_bb_$(RFS_TYPE)
rootfs_br: rootfs_br_$(RFS_TYPE)
rootfs_bs: rootfs_bs_$(RFS_TYPE)

RFS_DEP_bb	= $(O_BB_DIR)/.build_done.busybox
RFS_DEP_br	=
RFS_DEP_bs	=

ifeq ($(ARM_TEE),1)
RFS_DEP_bb	+= $(O_OTCL_DIR)/.build_done.optee_client
RFS_DEP_br	+= $(O_OTCL_DIR)/.build_done.optee_client
endif


ROOTFS_BB_DIR = $(EEL_OUT_DIR)/$(ARCH)/$(BB_VER_DIR)/rootfs_$(BB_CONFIG)
PHONY-rootfs += rootfs_bb_cpio rootfs_bb_ext4 rootfs_bb_erofs
rootfs_bb_cpio: $(ROOTFS_BB_DIR)
	$(EELQ)\cd $(ROOTFS_BB_DIR) \
		&& find ./* | $(FAKEROOT) $(CPIO) -H newc -o \
			> $(RFS_IMG_DIR_bb)/$(RFS_IMG_BASE_bb).cpio

rootfs_bb_ext4: $(ROOTFS_BB_DIR)
	$(EELQ4)\rm -f $(RFS_IMG_DIR_bb)/$(RFS_IMG_BASE_bb).ext4
	$(EELQ)$(MKEXT4) -r 1 -N 0 -m 5 -L "rootfs" -I 256 -O ^64bit \
		-d $(ROOTFS_BB_DIR) \
		$(RFS_IMG_DIR_bb)/$(RFS_IMG_BASE_bb).ext4 \
		128m

rootfs_bb_erofs: $(ROOTFS_BB_DIR)
	$(EELQ)$(MKEROFS) $(RFS_IMG_DIR_bb)/$(RFS_IMG_BASE_bb).erofs \
		$(ROOTFS_BB_DIR)/

# update ROOTFS_BB_DIR if RFS_DEP_bb updated
$(ROOTFS_BB_DIR): $(RFS_DEP_bb)
	$(EELQ5)$(MAKE_THIS) rootfs_bb_dir || \rm -fr $(ROOTFS_BB_DIR)

PHONY-rootfs += rootfs_bb_dir
rootfs_bb_dir: $(RFS_DEP_bb) $(RFS_TEE_DEPS)
	$(EELQ5)\rm -fr $(ROOTFS_BB_DIR)
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)
	@echo "temp rootfs dir: $(ROOTFS_BB_DIR)/"

	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/proc
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/sys
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/dev
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/tmp
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/root
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/home
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/var
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/mnt
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/lib
	$(EELQ5)mkdir -p $(ROOTFS_BB_DIR)/etc

	$(EELQ5)cp -r $(O_BB_DIR)/_install/* \
			$(ROOTFS_BB_DIR)/
	$(EELQ5)cp -r $(TC_LIBPATH_OS)/*so*	\
			$(ROOTFS_BB_DIR)/lib/
	$(EELQ5)cp -r $(EEL_CHG_DIR)/rootfs/bb-etc/* \
			$(ROOTFS_BB_DIR)/etc/

	$(EELQ5)$(FAKEROOT) mknod $(ROOTFS_BB_DIR)/dev/console c 5 1
	$(EELQ5)$(FAKEROOT) mknod $(ROOTFS_BB_DIR)/dev/null    c 1 3

ifeq ($(ARM_TEE),1)
	$(EELQ4)cp -r $(O_OTCL_DIR)/export/* \
			$(ROOTFS_BB_DIR)/

	$(EELQ4)cp -fr $(O_OTEX_DIR)/rfs_overlay/usr/* \
			$(ROOTFS_BB_DIR)/usr/
	$(EELQ4)cp -fr $(O_OTEX_DIR)/rfs_overlay/lib/* \
			$(ROOTFS_BB_DIR)/lib/
endif

PHONY-rootfs += rootfs_br_$(RFS_TYPE)
rootfs_br_$(RFS_TYPE): buildroot


PHONY-rootfs += rootfs_bs_$(RFS_TYPE)
PHONY-rootfs += rootfs_bs_ext4 rootfs_bs_cpio
# can not use FAKEROOT
rootfs_bs_ext4 rootfs_bs_cpio: $(O_BS_DIR).bootstrap_done bootstrap-post
	$(EELQ4)\rm -f $(RFS_IMG_DIR_bs)/$(RFS_IMG_BASE_bs).ext4
	$(EELQ)$(SUDO) $(MKEXT4) -r 1 -N 0 -m 5 -L "rootfs" -I 256 -O ^64bit \
		-d $(O_BS_DIR) \
		$(RFS_IMG_DIR_bs)/$(RFS_IMG_BASE_bs).ext4 \
		1G
	$(EELQ2)$(SUDO) chown $(USERNAME):$(USERNAME) $(RFS_IMG_DIR_bs)/$(RFS_IMG_BASE_bs).ext4

$(INITRD_FILE):
	$(EELQ5)$(MAKE_THIS) rootfs_bs_cpio
